﻿namespace SuperModel.Generation
{
    using System;
    using System.Collections.Generic;
    using System.Reflection.Emit;

    /// <summary>
    /// The interface implementer
    /// </summary>
    internal class InterfaceImplementer
    {
        /// <summary>
        /// The Property Implementer
        /// </summary>
        private readonly PropertyImplementer propertyImplementer = new PropertyImplementer();

        /// <summary>
        /// Implement an Interface
        /// </summary>
        /// <param name="interfaceToImplement">
        /// The interface to implement.
        /// </param>
        /// <param name="newType">
        /// The new type.
        /// </param>
        /// <param name="fields">
        /// The fields.
        /// </param>
        public void ImplementInterface(Type interfaceToImplement, TypeBuilder newType, List<FieldBuilder> fields)
        {
            newType.AddInterfaceImplementation(interfaceToImplement);

            // Implement the Events and Properties
            ImplementInterfaceProperties(newType, interfaceToImplement, fields);
        }

        /// <summary>
        /// Implement properties on an Interface
        /// </summary>
        /// <param name="newType">The type being constructed.</param>
        /// <param name="interfaceToImplement">the interface being implemented.</param>
        /// <param name="fields">The property fields.</param>
        private void ImplementInterfaceProperties(TypeBuilder newType, Type interfaceToImplement, IList<FieldBuilder> fields)
        {
            foreach (var currentProperty in interfaceToImplement.GetProperties())
            {
                // If the Current Property is readonly, Implement the Triggerable Property.
                // Implement a Basic Data Changing Property
                propertyImplementer.ImplementDataChangingProperty(newType, currentProperty, fields);
            }
        }
    }
}
